@echo off

set OLDDIR=%CD%


cd %1

IF EXIST skipdir GOTO skip_dir
cd %OLDDIR%

set OLDDIR=%CD%

@echo Entering: %1

copy %OLDDIR%\buildasm.bat %1\ > NUL
copy %OLDDIR%\buildc.bat %1\ > NUL
copy %OLDDIR%\buildcpp.bat %1\ > NUL

cd %1

IF EXIST asmbuild.txt copy %BUILDDIR%\buildmod.bat+asmbuild.txt+%BUILDDIR%\objcheck.bat buildasm.bat > NUL
IF EXIST asmbuild.txt @echo.  Using folder ASM script

IF EXIST cbuild.txt copy %BUILDDIR%\buildmod.bat+cbuild.txt+%BUILDDIR%\objcheck.bat buildc.bat > NUL
IF EXIST cbuild.txt @echo.  Using folder C script

IF EXIST cppbuild.txt copy %BUILDDIR%\buildmod.bat+cppbuild.txt+%BUILDDIR%\objcheck.bat buildcpp.bat > NUL
IF EXIST cppbuild.txt @echo.  Using folder CPP script



IF EXIST *.asm @echo.  Building ASM Files...
IF EXIST *.asm for /f %%a IN ('dir /b *.asm') do call buildasm.bat %%a %%~na %%~xa

IF EXIST *.c @echo.  Building C Files...
IF EXIST *.c for /f %%a IN ('dir /b *.c') do call buildc.bat %%a %%~na %%~xa

IF EXIST *.cpp @echo.  Building C++ Files...
IF EXIST *.cpp for /f %%a IN ('dir /b *.cpp') do call buildcpp.bat %%a %%~na %%~xa


:dir_build
IF EXIST %CD%\dir.build @echo.    Running directory script...
IF EXIST %CD%\dir.build COPY dir.build dirb.bat
IF EXIST %CD%\dir.build CALL dirb.bat
IF EXIST %CD%\dirb.bat DEL dirb.bat > NUL
IF EXIST skipdir GOTO skip_dir_final

IF EXIST buildasm.bat DEL buildasm.bat
IF EXIST buildc.bat DEL buildc.bat
IF EXIST buildcpp.bat DEL buildcpp.bat

@echo.

chdir /d %OLDDIR%

goto :end_dir

:skip_dir
IF EXIST %CD%\dir.build goto dir_build
:skip_dir_final
cd %OLDDIR%


:end_dir
